package com.lyj.mall.controller;

import com.lyj.mall.consts.MethodConst;
import com.lyj.mall.form.CartAddForm;
import com.lyj.mall.form.CartUpdateForm;
import com.lyj.mall.pojo.User;
import com.lyj.mall.service.CartService;
import com.lyj.mall.vo.CartVo;
import com.lyj.mall.vo.ResponseVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lyj
 * @date 2020/1/16 23:55
 */
@RestController
@Api(tags = "购物车模块Controller")
@RequestMapping(value = "/carts")
public class CartController extends BaseController {

    @Autowired
    private CartService cartService;

    @GetMapping
    public ResponseVo<CartVo> list() {
        this.insertLog("查询购物车", MethodConst.QUERY, "uid: " + user.getId());
        return cartService.list(user.getId());
    }

    @PostMapping
    public ResponseVo<CartVo> add(@Valid @RequestBody CartAddForm cartAddForm) {
        this.insertLog("购物车添加商品", MethodConst.INSERT, "cartAddForm: " + cartAddForm);
        return cartService.add(user.getId(), cartAddForm);
    }

    @PutMapping(value = "/{productId}")
    public ResponseVo<CartVo> put(@PathVariable("productId") Integer productId,
                                  @Valid @RequestBody CartUpdateForm cartUpdateForm) {
        this.insertLog("更新购物车", MethodConst.UPDATE, "productId: " + productId);
        return cartService.update(user.getId(), productId, cartUpdateForm);
    }

    @DeleteMapping(value = "/{productId}")
    public ResponseVo<CartVo> delete(@PathVariable("productId") Integer productId) {
        this.insertLog("删除购物车某商品", MethodConst.DELETE, "productId: " + productId);
        return cartService.delete(user.getId(), productId);
    }

    @PutMapping(value = "/selectAll")
    public ResponseVo<CartVo> selectAll() {
        this.insertLog("购物车全选", MethodConst.UPDATE, "uid: " + user.getId());
        return cartService.selectAll(user.getId());
    }

    @PutMapping(value = "/unSelectAll")
    public ResponseVo<CartVo> unSelectAll() {
        this.insertLog("购物车全不选", MethodConst.UPDATE, "uid: " + user.getId());
        return cartService.unSelectAll(user.getId());
    }

    @GetMapping(value = "/products/sum")
    public ResponseVo<Integer> sum() {
        this.insertLog("查询购物车商品总个数", MethodConst.QUERY, null);
        return cartService.sum(user.getId());
    }
}
